package com.jse.handler;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

@ControllerAdvice
public class ReturnValueHandler implements ResponseBodyAdvice<Object> {

	@Override
	public Object beforeBodyWrite(Object obj, MethodParameter returnType, MediaType mediaType,
			Class<? extends HttpMessageConverter<?>> converterType, ServerHttpRequest req, ServerHttpResponse resp) {
		System.err.println("返回值:"+obj);
		//通过 ServerHttpRequest的实现类ServletServerHttpRequest 获得HttpServletRequest  
        //ServletServerHttpRequest sshr=(ServletServerHttpRequest) serverHttpRequest;
        //此处获取到request 是为了取到在拦截器里面设置的一个对象 是我项目需要,可以忽略
        //HttpServletRequest request=   sshr.getServletRequest();

        //将返回值returnValue转成我需要的类型Message<?>  方便统一修改其中的某个属性  
       
		return obj;
	}

	@Override
	public boolean supports(MethodParameter type, Class<? extends HttpMessageConverter<?>> clazz) {
		System.err.println("诗人");
		//获取当前处理请求的controller的方法
        String methodName=type.getMethod().getName(); 
        // 不拦截/不需要处理返回值 的方法
        String method= "loginCheck"; //如登录
        //不拦截
		return !method.equals(methodName);
	}

}
